package com.tos.slidepuzzle;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.tos.utils.Constants;
import com.tos.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

public class StartingActivity extends Activity implements OnClickListener,
		OnTouchListener {

	private int backgroundColor;
	private RadioButton grayButton;
	private RadioButton blackButton;
	private RadioButton whiteButton;

	private Spinner rowSpinner;
	private int numOfRow;

	private Spinner colSpinner;
	private int numOfCol;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			View mView = getLayoutInflater().inflate(
					R.layout.starting_activity, null);
			mView.setOnTouchListener(this);
			setContentView(mView);

			AdView adview = (AdView) findViewById(R.id.adView);
			AdRequest re = new AdRequest();
			adview.loadAd(re);

			grayButton = (RadioButton) findViewById(R.id.grayButton);
			blackButton = (RadioButton) findViewById(R.id.blackButton);
			whiteButton = (RadioButton) findViewById(R.id.whiteButton);

			ArrayAdapter<CharSequence> adapter = ArrayAdapter
					.createFromResource(this, R.array.dimension_array,
							android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			rowSpinner = (Spinner) findViewById(R.id.rowSpinner);
			rowSpinner.setAdapter(adapter);
			rowSpinner.setPrompt("Select Dimension");

			colSpinner = (Spinner) findViewById(R.id.colSpinner);
			colSpinner.setAdapter(adapter);
			colSpinner.setPrompt("Select number of Columns");

			findViewById(R.id.startButton).setOnClickListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.startButton:
			goToGameScreen();
			break;
		}

	}

	private void goToGameScreen() {
		if (grayButton.isChecked()) {
			backgroundColor = R.color.background_gray;
		} else if (blackButton.isChecked()) {
			backgroundColor = R.color.background_black;
		} else {
			backgroundColor = R.color.background_white;
		}

		Utils.putBackgroundColor(this, Constants.KEY_BACKGROUND,
				backgroundColor);

		numOfRow = rowSpinner.getSelectedItemPosition() + 3;
		numOfCol = colSpinner.getSelectedItemPosition() + 3;
		Intent intent = new Intent(this, SlidePuzzleActivity.class);
		intent.putExtra(Constants.KEY_ROW, numOfRow);
//		intent.putExtra(Constants.KEY_COL, numOfCol);
		startActivity(intent);
	}

	@Override
	public boolean onTouch(View v, MotionEvent arg1) {
		if (!(v instanceof EditText)) {
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(v.getWindowToken(), 0);
		}
		return false;
	}
}